package collection.queue;

import org.junit.Test;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * 阻塞队列
 */
public class BlockQueuede {


    public static void main(String[] args) {


        ArrayBlockingQueue<String> blockingQueue = new ArrayBlockingQueue(1);


        blockingQueue.add("1");
        System.out.println(blockingQueue.peek());


        new Thread(() -> {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            String remove = blockingQueue.remove();
            System.out.println("移除" + remove);
        }).start();

        try {
            blockingQueue.put("2");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        try {
            System.out.println("hou" + blockingQueue.take());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }





    @Test
    public void ArrayBlockingQueueTest() throws InterruptedException {
        ArrayBlockingQueue<String> blockingQueue = new ArrayBlockingQueue(1);

        //offer ->offer
        blockingQueue.add("2");
        //enqueue
        blockingQueue.offer(null);
        blockingQueue.put(null);
        blockingQueue.offer(null, 0, null);

        //remove ->poll
        blockingQueue.remove();
        //dequeue
        blockingQueue.poll();
        blockingQueue.take();
        blockingQueue.poll(0, null);
    }





    /**
     * 链表有界队列
     * */
    @Test
    public void LinkedBlockingQueueTest() throws InterruptedException {

        System.out.println(3);

        LinkedBlockingQueue blockingQueue = new LinkedBlockingQueue();

        //offer ->offer
        blockingQueue.add("2");
        //enqueue
        blockingQueue.offer(null);
        blockingQueue.put(null);
        blockingQueue.offer(null, 0, null);

        //remove ->poll
        blockingQueue.remove();
        //dequeue
        blockingQueue.poll();
        blockingQueue.take();
        blockingQueue.poll(0, null);

    }
}
